package com.kuaimacode.kframework.admin.controller.category;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.category.UserCouponService;
import com.kuaimacode.kframework.admin.services.coupon.CouponInfoService;
import com.kuaimacode.kframework.admin.services.user.UserService;
import com.kuaimacode.kframework.admin.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.coupon.CouponInfo;
import com.kuaimacode.kframework.mybatis.models.coupon.UserCoupon;
import com.kuaimacode.kframework.mybatis.models.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2019/03/25
 */
/**
 * Controller
 */
@Controller
@RequestMapping({"/userCoupon"})
public class UserCouponController {
	private static String PAGE_FILE_NAME = "userCoupon/";

	@Autowired
	private UserCouponService userCouponServices;

	@Autowired
    private CouponInfoService couponInfoService;


	@Autowired
    private UserService userService;

	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						UserCoupon userCoupon,ModelMap model) {
		PageInfo<UserCoupon> pageInfo = userCouponServices.findAll(pageNum, pageSize,userCoupon);
		// pageInfo.list userCoupon
		model.put("pageInfo", pageInfo);
		model.put("userCouponSearch", userCoupon);
		Map map = new HashMap<>();
		for(Constant.DictStatusEnum d : Constant.DictStatusEnum.values()){
		    map.put(d.getCode(),d.getMessage());

        }
		model.put("state", map);
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		UserCoupon userCoupon = new UserCoupon();
		map.put("userCoupon", userCoupon);
		map.put("isAdd", true);
		//查询正常可使用的优惠券信息
        List<CouponInfo> coupons = couponInfoService.queryNormal();
        map.put("coupons",coupons);
		return PAGE_FILE_NAME+"form";
	}


	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		UserCoupon userCoupon = userCouponServices.getById(id);
		map.put("userCoupon", userCoupon);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}


    /**
     * 优惠券发放
     * @param userCoupon
     * @param isAdd
     * @param map
     * @return
     */
	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(UserCoupon userCoupon, Boolean isAdd,ModelMap map) {
		try {
			if(isAdd){
				// 新建保存
				userCouponServices.save(userCoupon);
			}else{
				// 编辑保存
				userCouponServices.updateById(userCoupon);
			}
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}

    /**
     * 优惠券发放
     * @param userCoupon
     * @param isAdd
     * @param map
     * @return
     */
    @RequestMapping(value = {"/send"}, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult send(UserCoupon userCoupon, String sendType,String userIds,ModelMap map) {
        try {
            if("1".equals(sendType)){//所有用户
                userCouponServices.sendAllUser(userCoupon);

            }else{//特定用户
                if(userIds!=null && !"".equals(userIds.trim())){
                    String[] user_phones = userIds.split(";");
                    for(String phone:user_phones){
                        User user = userService.selectByPhone(phone);
                        userCoupon.setUserId(user.getId());
                        userCoupon.setCreateAt(new Date());
                        userCoupon.setCreateBy(SessionUtil.getUserId());
                        userCoupon.setState(Integer.parseInt(Constant.COUPON_STATUS_NOT_USED));
                        userCouponServices.save(userCoupon);

                    }
                }else{
                    return JsonResult.failure("请输入用户手机号");

                }


            }



        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }




    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			userCouponServices.removeById(id);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}

}
